#include "cmenustate.h"
#include "cgamestate.h"
#include <QGLWidget>
#include <QMessageBox>
#include <iostream>
#include "math.h"

CMenuState CMenuState::st_menu;

void CMenuState::draw(QGLWidget * gl)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glColor3f(1.0f, 1.0f, 1.0f);
    glBindTexture(GL_TEXTURE_2D,texture[0]);
    glBegin(GL_QUADS);  // fonas
        glTexCoord2f(0.0f,0.0f); glVertex3i(  0, 0,-100);
        glTexCoord2f(0.0f,1.0f); glVertex3i(  0, 600,-100);
        glTexCoord2f(1.0f,1.0f); glVertex3i(800, 600,-100);
        glTexCoord2f(1.0f,0.0f); glVertex3i(800, 0,-100);
    glEnd();

    static float pulse = 0.0f;
    pulse += 0.1f;
    float in = 0.75f + sin( pulse )/8.0f;

    glColor3f(1.0f, 1.0f, 1.0f );
    foreach ( TMLabel l, txt[0] ) {
        gl->renderText( l.x, l.y, l.text, font );
    }
    foreach ( TMLabel l, myg[0] ) {
        if ( stateSelection[0] == l.action ) glColor3f(in, in, in ); else glColor3f(1.0f, 1.0f, 1.0f );
        gl->renderText( l.x, l.y, l.text, font );
    }
}

void CMenuState::enterState()
{
    std::cout << "! Aktyvuotas Meniu state " << std::endl;
}

void CMenuState::leaveState()
{
    std::cout << "- Deaktyvuotas Meniu state " << std::endl;
}

void CMenuState::update()
{

}

void CMenuState::initVars()
{
    menuState = 0;
    stateSelection[0] = 0;
    stateSelection[1] = 4;

    font = QFont("Verdana", 20, 0, 0 );

    // pagrindinis meniu
    txt[0].append( TMLabel(QString::fromStdWString(L"Biliardas"), 300, 50, font, -1 ) );
    myg[0].append( TMLabel(QString::fromStdWString(L"Naujas žaidimas"), 300, 100, font, 0 ) );
    myg[0].append( TMLabel(QString::fromStdWString(L"Tęsti žaidimą…"), 300, 150, font, 1) );
    myg[0].append( TMLabel(QString::fromStdWString(L"Nustatymai"), 300, 200, font, 2) );
    myg[0].append( TMLabel(QString::fromStdWString(L"Baigti"), 300, 250, font, 3) );
}

void CMenuState::initGL(QGLWidget * gl)
{
    //gl->makeCurrent();
    texture[0] = gl->bindTexture(QPixmap(QString("../img/MAX0010.jpg")), GL_TEXTURE_2D);
    glEnable(GL_TEXTURE_2D);
    //glColor4f(1.0f,1.0f,1.0f,1.0f);
    //glEnable(GL_BLEND);
    //glEnable(GL_ALPHA_TEST);
    //glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
    //glAlphaFunc(GL_GREATER,0.1f);
}

int CMenuState::doLogic(QList<int> Kl)
{
    if( Kl.contains(Qt::Key_Down) && stateSelection[menuState] < myg[menuState].size()-1 ) {  //  Ć…Ā¾ymĆ„ā€”jimas klaviatĆ…Ā«ra
        stateSelection[menuState]++;
    } else if( Kl.contains(Qt::Key_Up) && stateSelection[menuState] > 0 ) {
        stateSelection[menuState]--;
    } else if (Kl.contains(Qt::Key_Escape)) {
        if (p_stMngr != NULL)
           changeState(p_stMngr, CGameState::instance());
    } else if (Kl.contains(Qt::Key_Return)) {
        if (menuState == 0 && stateSelection[menuState] == 0)
            if (p_stMngr != NULL)
                changeState(p_stMngr, CGameState::instance());
        if (menuState == 0 && stateSelection[menuState] == 3)
            return -1;

    }
    return 0;
}
